/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.action;

import bookstore.dao.CategoryDao;
import bookstore.iservice.ICategoryService;
import bookstore.model.Category;
import bookstore.service.CategoryService;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "CategoryAction", urlPatterns = {"/CategoryAction"})
public class CategoryAction extends HttpServlet {

    private ICategoryService categoryService;

    public String getDdName() {
        return ddName;
    }

    public void setDdName(String ddName) {
        this.ddName = ddName;
    }
    private String ddName;
    

    public CategoryAction() {
        categoryService = new CategoryService();
        categoryService.setCategoryDao(new CategoryDao());
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=gbk");
        String method = request.getParameter("method");
        if (method.equals("manage")) {
            manage(request, response);
        } else if (method.equals("add")) {
            add(request, response);
        } else if (method.equals("willEdit")) {
            willEdit(request, response);
        } else if (method.equals("edit")) {
            edit(request, response);
        } else if (method.equals("delete")) {
            delete(request, response);
        }
    }

    protected void manage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Category> list = categoryService.findAllCategories();
        request.setAttribute("list", list);
        RequestDispatcher rd = request.getRequestDispatcher("admin/categoryManage.jsp");
        rd.forward(request, response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Category category = new Category();
        category.setName(request.getParameter("addName"));
        categoryService.addCategory(category);
        RequestDispatcher rd = request.getRequestDispatcher("CategoryAction?method=manage");
        rd.forward(request, response);
    }

    protected void willEdit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.findCategoryById(id);
        request.setAttribute("category", category);
        //categoryService.editCategory(category);
        RequestDispatcher rd = request.getRequestDispatcher("admin/categoryEdit.jsp");
        rd.forward(request, response);
    }

    protected void edit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.findCategoryById(id);
        category.setName(request.getParameter("name"));
        categoryService.editCategory(category);
        RequestDispatcher rd = request.getRequestDispatcher("CategoryAction?method=manage");
        rd.forward(request, response);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        categoryService.deleteCategory(id);
        RequestDispatcher rd = request.getRequestDispatcher("CategoryAction?method=manage");
        rd.forward(request, response);
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
